package com.huangd.platform.common.utils;

import org.apache.commons.lang.StringUtils;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DataUtils {

	public static final int ORDER_ASC = 1;
	public static final int ORDER_DESC = -1;

	public static long calc(List<Map<String, String>> list, String key) {

		long v = 0;

		if (list != null && list.size() > 0) {
			for (Map<String, String> map : list) {
				String val = map.get(key);
				if (val != null) {
					v += Long.parseLong(val);
				}
			}
		}

		return v;
	}

	public static void percent(List<Map<String, String>> list, String key, String calcKey) {

		long total = calc(list, calcKey);

		if (list != null && list.size() > 0) {
			for (Map<String, String> map : list) {
				String val = map.get(calcKey);
				double d = val == null ? 0 : MathUtils.divideAndRoundDown(Long.parseLong(val) * 100, total, 2);
				map.put(key, String.valueOf(d));
			}
		}
	}


	public static Set<String> setOf(List<Map<String, Object>> list, String key) {

		Set<String> set = new HashSet<>();

		if (list == null || list.size() == 0)
			return set;

		for (Map<String, Object> obj : list) {
			String v = obj.get(key).toString();
			if (Utils.isEmpty(v))
				continue;

			set.add(v);
		}

		return set;
	}

	public static Map<String, Object> mapOf(List<Map<String, Object>> list, String key, String valueKey) {

		Map<String, Object> result = new HashMap<>();

		if (list == null || list.size() == 0)
			return result;

		for (Map<String, Object> obj : list) {
			String v = DataUtils.objToStr(obj.get(key));
			if (Utils.isEmpty(v))
				continue;

			result.put(v, obj.get(valueKey));
		}

		return result;
	}

	public static String getValue(Map<String, String> datas, String key) {
		return getValue(datas, key, null);
	}

	public static String getValue(Map<String, String> datas, String key, String defaultValue) {

		String v = datas.get(key);

		return v == null ? defaultValue : v;
	}

	public static String getValueUnique(List<Map<String, String>> datas, String key) {
		return getValueUnique(datas, key, null);
	}

	public static String getValueUnique(List<Map<String, String>> datas, String key, String defaultValue) {

		if (datas == null || datas.size() == 0)
			return defaultValue;

		Map<String, String> map = datas.get(0);
		String v = map.get(key);

		return v == null ? defaultValue : v;
	}

	public static int getIntValue(Map<String, Object> datas, String key) {
		return getIntValue(datas, key, 0);
	}

	public static int getIntValue(Map<String, Object> datas, String key, int defaultValue) {

		Object v = datas.get(key);

		return v == null ? defaultValue : Integer.parseInt(v == null ? "0" : v.toString());
	}

	public static long getLongValue(Map<String, Object> datas, String key) {
		return getLongValue(datas, key, 0);
	}

	public static long getLongValue(Map<String, Object> datas, String key, long defaultValue) {

		String v = DataUtils.nullToStr(datas.get(key));

		return StringUtils.isBlank(v) ? defaultValue : Long.parseLong(v);
	}

	public static int getIntValueUnique(List<Map<String, Object>> datas, String key) {
		return getIntValueUnique(datas, key, 0);
	}

	public static int getIntValueUnique(List<Map<String, Object>> datas, String key, int defaultValue) {

		if (datas == null || datas.size() == 0)
			return defaultValue;

		Map<String, Object> map = datas.get(0);
		String v = map.get(key).toString();

		return v == null ? defaultValue : Integer.parseInt(v);
	}

	public static String join(Collection<?> c, String concat, String every) {

		StringBuilder builder = new StringBuilder();
		int count = 0;
		for (Object obj : c) {
			if (count++ > 0) {
				builder.append(concat);
			}

			builder.append(every);
			builder.append(obj);
		}
		return builder.toString();
	}

	/**
	 * 去除换行,制表符,空格,回车
	 */
	public static String updateStr (String s) {
		Pattern p = Pattern.compile("\\s*|\t|\r|\n");
		Matcher m = p.matcher(s);
		s = m.replaceAll("");
		return s;
	}

	/**
	 * 换行变逗号
	 */
	public static String updateComma(String s) {
		if (StringUtils.isNotBlank(s)){
			Pattern p = Pattern.compile("\\s*\n");
			Matcher m = p.matcher(s);
			s = m.replaceAll(",");
		}

		return updateStr(s);
	}

	/**
	 * 换行变逗号
	 */
	public static String updateBar(String s) {
		if (StringUtils.isNotBlank(s)){
			Pattern p = Pattern.compile("\\s*\n");
			Matcher m = p.matcher(s);
			s = m.replaceAll("|");
		}

		return updateStr(s);
	}

	//获取两数组交集
	private static Set<Integer> getIntersection(Integer[] channel1, Integer[] channel2) {
		Set<Integer> loginChannels = new HashSet<Integer>();
		for (int i = 0; i < channel1.length; i++) {
			for (int j = 0; j < channel2.length; j++) {
				if (channel1[i] == channel2[j]) {
					loginChannels.add(channel1[i]);
				}
			}
		}
		return loginChannels;
	}

	public static String nullToStr (Object str) {
		return str == null ? "" : str.toString();
	}

	public static String objToStr(Object obj) {
		return obj == null ? "" : obj.toString();
	}

}
